<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class IdiomController extends MessageController {


    /**
     * 缓存到文件
     */
    private function cacheidiom(){

        $cache = S('cacheidiom');

        if (!empty($cache)){
            return $cache;
        }

        $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cacheidiom',$data,7200);

        return S('cacheidiom');

    }

    public function delidiom(){

        exit;
        $cache = S('cacheidiom');

        foreach ($cache as $value){

            $where['wxopen_template_send_id'] = $value['wxopen_template_send_id'];
            M('idiom_template_send')->where($where)->delete();
        }

        exit('ok');

    }


    /**
     * 恶搞
     */
    public function sendidiom(){


       $info = $this->cacheidiom();
//        $where['status'] = '-1';
//        $where['touser'] = 'ojI_b4gCRntlXJQCEU_nQhIYFDOg';
//        $info = M('spoof_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($where)->limit(1)->order('wxopen_template_send_id asc')->select();

        $appid = "wx71e7a983a194c512";
        $seret = "5b4e4315c5064fe780f46cbde2ed6ba6";
        $temp = "vCNMFGGqF4DMOhJ6gGSrNY94WWWaoDI6JI4XtWaTYNA";

        foreach ($info as $key=>$value){


            $fdata = array(
                'keyword1'=>array(
                    'value'=>'千元大奖已送出',
                    'color'=>'#ff0000',
                ),
                'keyword2'=>array(
                    'value'=>'端午节有奖活动',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'端午有奖活动圆满结束，谢谢朋友们的支持，没拿到奖的朋友们我们下次再约！',
                    'color'=>'#173177',
                ),

            );

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$temp,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword1.DATA'
            );
            $res = $this->sendtemplate($appid,$seret,$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
               // M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                //M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }
        echo 'ok';
        exit;
    }

}